

// NodeJs 链接 MongoDB 数据库 
// Mongoose 插件  

const mongoose = require('mongoose')

const hostname = "localhost"
const port = 27017
const database = "wh2209"
const user = "?"
const pwd = "?"

const CONN_DB_URL = `mongodb://${hostname}:${port}/${database}`
console.log(CONN_DB_URL)


// 链接成功
mongoose.connect(CONN_DB_URL,(err)=>{
    if(err){
        console.log('数据库链接失败')
        throw err;
    }else{
        console.log("数据库链接成功了")
    }
})

const connection = mongoose.connection  // 数据库的链接对象  

connection.on('disconntect',()=>{
    console.log('数据库断开链接了')
})

connection.on('open',()=>{
    console.log('数据库打开链接了')
})

connection.on('close',()=>{
    console.log('数据库关闭了')
})

connection.on('error',(err)=>{
    console.log(err)
    console.log('数据库链接异常')
})